﻿Public Class Chicken
    Dim xChickenOld As Integer
    Dim Health As Integer
    Dim xHungry As Boolean
    Dim xFeedCount As Integer
    Dim xLayed As Integer
    Dim xPos As Integer
    Public Property ChickenOld As Integer
        Get
            Return xChickenOld
        End Get
        Set(value As Integer)
            xChickenOld = value
        End Set
    End Property
    Public Property FeedCount As Integer
        Get
            Return xFeedCount
        End Get
        Set(value As Integer)
            xFeedCount = value
        End Set
    End Property
    Public Property Layed As Integer
        Get
            Return xLayed
        End Get
        Set(value As Integer)
            xLayed = value
        End Set
    End Property
    Public Property Pos As Integer
        Get
            Return xPos
        End Get
        Set(value As Integer)
            xPos = value
        End Set
    End Property
    Public Sub TimerChickenHunger_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerChickenHunger.Tick
        xHungry = True
        ChickenDamage()
    End Sub
    Public Sub ChickenDamage()
        HealthMeter.BackColor = Color.DarkRed
        HealthMeter.Height -= 5
        HealthMeter.Top += 5
        Health += 5
        If HealthMeter.Height <= 0 Then
            Me.Hide()
            'minus chicken total
            Parent.Controls.OfType(Of Label)().ElementAt(3).Text -= 1

            If Parent.Controls.OfType(Of Label)().ElementAt(4).Text = 0 And Parent.Controls.OfType(Of Label)().ElementAt(3).Text = 0 Then
                Me.TimerBiten.Stop()
                Me.TimerChickenHunger.Stop()
                Dim Obj As New Point
                Obj.PassedText = Parent.Controls.OfType(Of Label)().ElementAt(0).Text
                Obj.HighScore = Parent.Controls.OfType(Of Label)().ElementAt(9).Text
                Obj.Killeds = Convert.ToInt64(Parent.Controls.OfType(Of Label)().ElementAt(1).Text)

                Obj.Show()
            End If

            Me.Dispose()
        End If
    End Sub
    Public Sub ChickenFeeded()
        HealthMeter.BackColor = Color.ForestGreen
        HealthMeter.Height = 45
        HealthMeter.Top = 15
        xFeedCount += 1
        If Not ChickenOld = 4 Then
            Grow(xFeedCount)
        End If
    End Sub
    Public Sub Grow(ByVal FeedCount As Integer)
        If FeedCount >= 5 Then
            ChickenOld = 3
            Me.ChickenImage.Image = My.Resources.chicken
            Me.TimerLay.Enabled = True
        ElseIf FeedCount >= 2 Then
            ChickenOld = 2
            Me.ChickenImage.Image = My.Resources.mediumchicken
        Else
            ChickenOld = 1
            Me.ChickenImage.Image = My.Resources.littlechicken
        End If
    End Sub
    Public Sub HarvestEgg()
        'Disposing Object Egg
        Dim ObjEgg As Object
        For Each ObjEgg In Me.Controls
            If TypeOf ObjEgg Is Harvest Then
                ObjEgg.Dispose()
            End If
        Next
        'LabelScore
        If ChickenOld >= 4 Then
            Parent.Controls.OfType(Of Label)().ElementAt(0).Text += 200
        Else
            Parent.Controls.OfType(Of Label)().ElementAt(0).Text += 100
        End If
        TimerLay.Start()
    End Sub
    Private Sub TimerLay_Tick(sender As Object, e As EventArgs) Handles TimerLay.Tick
        TimerLay.Interval = 5000
        For Each ObjEgg In Me.Controls
            If TypeOf ObjEgg Is Harvest Then
                ObjEgg.Dispose()
            End If
        Next
        Dim x As New Harvest
        'Adding handler for each x
        AddHandler x.Click, AddressOf HarvestEgg

        'Checking layed status and increment the layed value
        If Layed >= 10 Then
            ChickenOld = 4
        End If
        Layed += 1

        If ChickenOld = 4 Then
            x.ShowHarvest("EggGold")
        Else
            x.ShowHarvest("Egg")
        End If
        Me.Controls.Add(x)
        TimerLay.Stop()
    End Sub
    Private Sub ChickenImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChickenImage.Click
        If xHungry = True Then
            ChickenFeeded()
            xHungry = False
        End If
    End Sub
    Private Sub Chicken_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If ChickenOld >= 3 Then
            'set dis to false before open main.vb[design]
            Me.TimerLay.Enabled = True
        Else
            Me.TimerLay.Enabled = False
        End If
    End Sub
    Private Sub TimerBiten_Tick(sender As Object, e As EventArgs) Handles TimerBiten.Tick
        xHungry = True
        ChickenDamage()
    End Sub

End Class
